package com.cskaoyan.service;

import com.cskaoyan.config.UsernameUtils;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.model.bean.Goods;
import com.cskaoyan.model.vo.searchvo.*;
import com.cskaoyan.model.vo.systemvo.OrderBean;
import com.cskaoyan.model.vo.systemvo.UserIndexVo;
import com.mysql.jdbc.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public UserIndexVo userIndex() {
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        UserIndexVo userIndexVo = new UserIndexVo();
        OrderBean orderBean = new OrderBean();
        if (!UsernameUtils.isEmpty(username)){
            int id = userMapper.selectIdByUsername(username);
            orderBean.setUnpaid(userMapper.selectNumOfStatusByUserId(id,101));
            orderBean.setUnship(userMapper.selectNumOfStatusByUserId(id,201));
            orderBean.setUnrecv(userMapper.selectNumOfStatusByUserId(id,301));
            orderBean.setUncomment(userMapper.selectNumOfStatusByUserId(id,401));
            userIndexVo.setOrder(orderBean);
            return userIndexVo;
        }
        orderBean.setUncomment(0);
        orderBean.setUnrecv(0);
        orderBean.setUnship(0);
        orderBean.setUnpaid(0);
        return userIndexVo;
    }

    @Override
    public SearchIndexVo searchIndex() {
        SearchIndexVo searchIndexVo = new SearchIndexVo();
        searchIndexVo.setDefaultKeyword(userMapper.selectOneKeyword());
        searchIndexVo.setHotKeywordList(userMapper.selectHotKeyword());
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        if (!UsernameUtils.isEmpty(username)){
            int id = userMapper.selectIdByUsername(username);
            List<String> historyKeywords = userMapper.selectAllHistoryKeyword(id);
            ArrayList<HistoryKeyword> historyKeywords1 = new ArrayList<>();
            for (String historyKeyword : historyKeywords) {
                HistoryKeyword keyword = new HistoryKeyword();
                keyword.setKeyword(historyKeyword);
                historyKeywords1.add(keyword);
            }
            searchIndexVo.setHistoryKeywordList(historyKeywords1);
        }
        return searchIndexVo;
    }

    @Override
    public WxGoodsListVo goodsList(Integer page, Integer limit, String sort, String order, String keyword, Integer categoryId) {
        WxGoodsListVo wxGoodsListVo = new WxGoodsListVo();
        ArrayList<FilterCategory> filterCategories = new ArrayList<>();
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        if (!UsernameUtils.isEmpty(username)){
            int id = userMapper.selectIdByUsername(username);
            //查询有无重复
            List<String> keywords = goodsMapper.selectAllKeywordById(id);
            boolean flag = false;
            for (String key : keywords) {
                if (key.equals(keyword)){
                    flag = true;
                    break;
                }
            }
            //插入历史记录
            if (!flag){
                goodsMapper.insertHistory(id, keyword);
            }
        }

        keyword = "%" + keyword + "%";
        List<Integer> repeatIds = goodsMapper.selectCategoryIdByKeyword(keyword, limit, sort, order);
        HashSet<Integer> categoryIds = new HashSet<>(repeatIds);
        for (Integer cid : categoryIds) {
            FilterCategory filterCategory = goodsMapper.selectAllById(cid);
            filterCategories.add(filterCategory);
        }
        wxGoodsListVo.setFilterCategoryList(filterCategories);

        if (categoryId == 0){
            int count = goodsMapper.selectNumByKeyword(keyword, limit, sort, order);
            wxGoodsListVo.setCount(count);
            List<GoodsVo> goodsVos = goodsMapper.selectGoodsByKeyword(keyword, limit, sort, order);
            wxGoodsListVo.setGoodsList(goodsVos);
            return wxGoodsListVo;
        }

        int count = goodsMapper.selectNumByKeywordAndCategoryId(keyword, limit, sort, order,categoryId);
        wxGoodsListVo.setCount(count);
        List<GoodsVo> goodsVos = goodsMapper.selectGoodsByKeywordAndCategoryId(keyword, limit, sort, order,categoryId);
        wxGoodsListVo.setGoodsList(goodsVos);
        return wxGoodsListVo;


    }

    @Override
    public void clearHistory() {
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        int id = userMapper.selectIdByUsername(username);

        goodsMapper.deleteHistory(id);
    }
}
